package com.xposed.aliproxy.http;

import com.xposed.aliproxy.utils.XposeUtil;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.protocol.HTTP;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public class HttpBase {

    public static String m2919a(String str, String str2, String str3) throws Exception {
        if (str3 == null) {
            str3 = HTTP.UTF_8;
        }
        HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(str).openConnection();
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);
        httpURLConnection.setReadTimeout(60000);
        httpURLConnection.setRequestMethod(HttpPost.METHOD_NAME);
        httpURLConnection.setRequestProperty("Content-type", "application/json;charset=utf-8");
        httpURLConnection.setRequestProperty(HttpHeaders.ACCEPT_CHARSET, "utf-8");
        httpURLConnection.connect();
        DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
        dataOutputStream.write(str2.getBytes(str3));
        dataOutputStream.flush();
        dataOutputStream.close();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), HTTP.UTF_8));
        str3 = "";
        while (true) {
            String readLine = bufferedReader.readLine();
            if (readLine != null) {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(str3);
                stringBuilder.append(readLine);
                str3 = stringBuilder.toString();
            } else {
                httpURLConnection.disconnect();
                XposeUtil.xposedLog("httpRequest->", str2);
                XposeUtil.xposedLog("httpResponse->", str3);
                return str3;
            }
        }
    }

    public static String m2918a(String str) throws Exception {
        HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(str).openConnection();
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);
        httpURLConnection.setReadTimeout(60000);
        httpURLConnection.setRequestProperty("Content-type", "text/html");
        httpURLConnection.setRequestProperty(HttpHeaders.ACCEPT_CHARSET, HTTP.UTF_8);
        httpURLConnection.setRequestProperty("contentType", HTTP.UTF_8);
        httpURLConnection.setRequestMethod("GET");
        httpURLConnection.connect();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
        String str2 = "";
        while (true) {
            String readLine = bufferedReader.readLine();
            if (readLine != null) {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(str2);
                stringBuilder.append(readLine);
                str2 = stringBuilder.toString();
            } else {
                httpURLConnection.disconnect();
                PrintStream printStream = System.err;
                StringBuilder stringBuilder2 = new StringBuilder();
                stringBuilder2.append("request:");
                stringBuilder2.append(str);
                printStream.println(stringBuilder2.toString());
                PrintStream printStream2 = System.err;
                StringBuilder stringBuilder3 = new StringBuilder();
                stringBuilder3.append("response:");
                stringBuilder3.append(str2);
                printStream2.println(stringBuilder3.toString());
                return str2;
            }
        }
    }

    /* renamed from: a */
    public static String request(String str, Map<String, String> map) throws Exception {
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(str);
        postMethod.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");
        NameValuePair[] nameValuePairArr = new NameValuePair[map.size()];
        int i = 0;
        for (String str2 : map.keySet()) {
            nameValuePairArr[i] = new NameValuePair(str2, (String) map.get(str2));
            i++;
        }
        postMethod.setRequestBody(nameValuePairArr);
        str = "";
        if (httpClient.executeMethod(postMethod) == 200) {
            InputStream responseBodyAsStream = postMethod.getResponseBodyAsStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(responseBodyAsStream, HTTP.UTF_8));
            while (true) {
                String readLine = bufferedReader.readLine();
                if (readLine == null) {
                    break;
                }
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(str);
                stringBuilder.append(readLine);
                str = stringBuilder.toString();
            }
            responseBodyAsStream.close();
            bufferedReader.close();
        }
        postMethod.releaseConnection();
        PrintStream printStream = System.err;
        StringBuilder stringBuilder2 = new StringBuilder();
        stringBuilder2.append("retData->");
        stringBuilder2.append(str);
        printStream.println(stringBuilder2.toString());
        return str;
    }
}
